<template>
  <div>
    <el-dialog :title="title" :visible.sync="opens" width="1200px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-divider content-position="center">订单信息</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="工作号" prop="job_number">
              <el-select v-model="form.job_number" filterable placeholder="请选择工作号" clearable style="width: 100%" @change="jobNumberChange" disabled>
                <el-option
                  v-for="(dict,index) in numberAll"
                  :key="index"
                  :label="dict"
                  :value="dict"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="委托日期">
              <el-input v-model="order.entrust_date" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="内部类型">
              <el-input v-model="order.inside_type_name" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="主单号">
              <el-input v-model="order.main_doc_no" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分单号">
              <el-input v-model="order.sub_order_No" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="体积">
              <el-input v-model="order.volume" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="center">发票录入</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="发票号" prop="invoice_num">
              <el-input v-model="form.invoice_num" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结算单位" prop="job_number">
              <el-select v-model="form.settlement_unit" filterable placeholder="请选择结算单位" clearable style="width: 100%" disabled>
                <el-option
                  v-for="(dict,index) in unitAll"
                  :key="index"
                  :label="dict.account_name"
                  :value="dict.account_name"
                />
              </el-select>
            </el-form-item>
            <!--            <el-form-item label="结算单位" prop="settlement_unit">-->
            <!--              <el-input v-model="form.settlement_unit" />-->
            <!--            </el-form-item>-->
          </el-col>
          <el-col :span="8">
            <el-form-item label="发票抬头" prop="invoice_header">
              <el-input v-model="form.invoice_header" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="纳税识别号" prop="taxpayer">
              <el-input v-model="form.taxpayer" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" prop="telephone">
              <el-input v-model="form.telephone" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="开户行" prop="bank">
              <el-input v-model="form.bank" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="银行账号" prop="account">
              <el-input v-model="form.account" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发票类型">
              <el-select v-model="form.invoice_type" placeholder="请选择发票类型" style="width: 100%;" disabled>
                <el-option
                  v-for="item in invoice_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="开票公司" prop="invoicing_company">
              <el-input v-model="form.invoicing_company" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发票状态">
              <el-select v-model="form.invoice_status" placeholder="请选择发票状态" style="width: 100%;" disabled>
                <el-option
                  v-for="item in invoice_status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开票要求" prop="invoicing_ask">
              <el-input v-model="form.invoicing_ask" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="流水号" prop="stream_num">
              <el-input v-model="form.stream_num" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="人民币大写" prop="rmb_dx">
              <el-input v-model="form.rmb_dx" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="人民币小写" prop="rmb_xe">
              <el-input v-model="form.rmb_xe" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="核销日期" prop="cancel_date">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.cancel_date" type="date" placeholder="核销日期" style="width: 100%;" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="汇率" prop="exchange_rate">
              <el-select v-model="form.exchange_rate" placeholder="请选择汇率" clearable style="width: 100%" disabled>
                <el-option
                  v-for="(dict,index) in rateAll"
                  :key="index"
                  :label="'源币种('+dict.source_currency_text+')--目标币种('+dict.target_currency_text+')--汇率('+dict.rate+')'"
                  :value="dict.rate"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="税率" prop="tax_rate">
              <el-select v-model="order.tax_rate" placeholder="税率" clearable style="width: 100%" disabled>
                <el-option label="0.01" value="0.01" />
                <el-option label="0.03" value="0.03" />
                <el-option label="0.06" value="0.06" />
                <el-option label="0.10" value="0.10" />
                <el-option label="0.13" value="0.13" />
                <el-option label="0.17" value="0.17" />
              </el-select>
            </el-form-item>
            <!--            <el-input type="number" v-model="form.tax_rate" />-->
          </el-col>
        </el-row>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
<!--            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>-->
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { invoiceAdd, invoiceEdit, getJobNumber, getJobNumberInfo } from '@/api/settlement'
import { getProvider, getRate } from '@/api/basics'
export default {
  name: 'Detail',
  props: {
    opens: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      // 订单详情
      order: {},
      numberAll: [],
      invoice_type: [{
        value: '1',
        label: '货运代理发票'
      }
      ],
      invoice_status: [{
        value: '1',
        label: 'NEW'
      }
      ],
      //汇率
      rateAll: [],
      unitAll:[],
      rules: {
        // company_code: [
        //   { required: true, message: '请填写公司代码', trigger: 'blur' }
        // ],
        // name_c: [
        //   { required: true, message: '请填写中文名称', trigger: 'blur' }
        // ]
      }
    }
  },
  created() {
    this.getProvider()
    this.getJobNumber()
    this.getRate()
  },
  watch: {
    'opens': function(newValue, oldValue) {
      if (newValue === true && this.title === '详情') {
        this.jobNumberChange(this.form.job_number)
      }
    }
  },
  methods: {
    getRate() {
      getRate().then(response => {
        if (response.code === 10000) {
          this.rateAll = response.data
        }
      })
    },
    getProvider() {
      getProvider({'role':'15'}).then(response => {
        if (response.code === 10000) {
          this.unitAll = response.data
        }
      })
    },
    getJobNumber() {
      getJobNumber().then(response => {
        if (response.code === 10000) {
          this.numberAll = response.data
        }
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      invoiceAdd(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      invoiceEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:opens', false)
    },
    jobNumberChange(value) {
      getJobNumberInfo({ job_number: value }).then(response => {
        if (response.code === 10000) {
          this.order = response.data
          console.log(response.data.entrust_company_name)
          this.form.invoice_header = response.data.entrust_company_info.account_name
          this.form.address = response.data.entrust_company_info.addr
          this.form.telephone = response.data.entrust_company_info.relation_phone
          this.form.taxpayer = response.data.entrust_company_info.tax
          this.form.bank = response.data.entrust_company_info.bank_name
          this.form.account = response.data.entrust_company_info.bank_code
        }
      })
    },
  }
}
</script>

